var isLogin = false
const app = getApp()
// 引用百度地图微信小程序JSAPI模块 
var bmap = require('../../libs/bmap-wx.js');
var preMapSel = 0 // 地图默认选择（前一个选择）
var landlordServiceNames = ['叫醒服务', '行李寄存', '洗衣服务', '票务服务', '租车服务 ', '车站/机场接送人服务', '商务服务', '订票服务', '早餐服务', '送餐服务', '一客一扫', '一天一扫', '两天一扫', '三天一扫', '四天一扫', '五天一扫', '六天一扫', '七天一扫', '一客一换', '一天一换', '两天一换', '三天一换', '四天一换', '五天一换', '六天一换', '七天一换']
var landlordServiceImgs = ['../../images/wake_up_service.png', '../../images/luggage_deposit.png', '../../images/laundry_wash_service.png', '../../images/tiket_business_service.png', '../../images/car_rental_service.png', '../../images/pick_up_service.png', '../../images/business_service.png', '../../images/reserve_service.png', '../../images/breakfast_service.png', '../../images/meal_delivery_service.png', '../../images/clean_up.png', '../../images/bed_sheet_change.png']

Page({
  /**
   * 页面的初始数据
   * https://blog.csdn.net/zhaoyazhi2129/article/details/53787695
   * https://blog.csdn.net/weixin_41941325/article/details/80761115
   */
  data: {
    bannerList: [],
    isF: true,
    isS: false,
    isT: false,
    isFo: false,
    top: 0,
    discount: null,
    originalPrice: '',
    price: '',
    roomTitle: '',
    bedroomNumber: 0,
    livingRoomNumber: 0,
    toiletNumber: 0,
    scroeEvaluation: [{
      src: '../../images/evaluate_checked.png'
    }, {
      src: '../../images/evaluate_checked.png'
    }, {
      src: '../../images/evaluate_checked.png'
    }, {
      src: '../../images/evaluate_checked.png'
    }, {
      src: '../../images/evaluate_checked.png'
    }],
    mapSelect: [{
      state: 0,
      isSelected: true
    }, {
      state: 1,
      isSelected: false
    }, {
      state: 2,
      isSelected: false
    }, {
      state: 3,
      isSelected: false
    }],
    // map_enable: false,
    top: 0,
    isDess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //7734
    this.getIntentData(options)
    this.connectNetForData()

  },
  /**
   * 滑动监听事件
   */
  scroll: function(e) {
    let that = this
    console.log(e.detail.scrollTop)
    that.setData({
      top: e.detail.scrollTop
    })
  },
  summarizeClick: function(e) {
    this.setData({
      to_view: 'summarize',
      isF: true,
      isS: false,
      isT: false,
      isFo: false
    })
  },
  evaluateClick: function(e) {
    this.setData({
      to_view: 'envaluate',
      isF: false,
      isS: true,
      isT: false,
      isFo: false
    })
  },
  locationClick: function(e) {
    this.setData({
      to_view: 'map_info',
      isF: false,
      isS: false,
      isT: true,
      isFo: false
    })
  },

  noticeClick: function(e) {
    this.setData({
      to_view: 'check_in_tip',
      isF: false,
      isS: false,
      isT: false,
      isFo: true
    })
  },
  /**
   * 连接网络获取房源详情信息
   */
  connectNetForData() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    console.log(app.baseNet + 'roomInfo/queryRoomInfoById?sriRid=' + that.data.sriRid + '&username=' + that.data.username)
    wx.request({
      url: app.baseNet + 'roomInfo/queryRoomInfoById',
      data: {
        sriRid: that.data.sriRid,
        username: that.data.username
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var roomDetails = res.data
          if (roomDetails.messcode == '001') {
            var room = roomDetails.roomDtos
            var roomInfo = room.roomInfo
            var landlordInfo = room.landlordInfo
            var user = room.user
            var originPrice = null
            if (roomInfo.discount) {
              originPrice = roomInfo.price / (roomInfo.discount / 10)
            }
            that.getLandlordService(room.landlordService)
            that.getLandlordAsk(room.landlordProhibitDto)
            var ffL = room.roomSupportingFacilitiesList.length
            if (ffL % 3 != 0) {
              ffL = ffL / 3 + 1
            } else {
              ffL = ffL / 3
            }

            console.log('ffL : ' + ffL)
            if (user) {
              if (!user.score) {
                user.score = 0
              }
              console.log('scroe:' + user.score)

              for (var i = 0; i < 5; i++) {
                var srcc = 'scroeEvaluation[' + i + '].src'
                if (i >= user.score) {
                  that.setData({
                    [srcc]: '../../images/evaluate_unchecked.png'
                  })
                }
              }
            }

            that.setData({
              bannerList: room.roomPictureList,
              discount: roomInfo.discount,
              price: '￥' + roomInfo.price,
              originalPrice: originPrice,
              dayTip: '/天',
              room: room,
              roomInfo: roomInfo,
              roomTitle: roomInfo.roomTitle,
              landlordInfo: landlordInfo,
              roomIntroduce: landlordInfo.introduction,
              whetherCheckTrue: roomInfo.whetherCheckTrue,
              score: roomInfo.score,
              roomMeasureArea: roomInfo.roomMeasureArea,
              leaseType: roomInfo.leaseType,
              bedroomNumber: roomInfo.bedroomNumber,
              livingRoomNumber: roomInfo.livingRoomNumber,
              toiletNumber: roomInfo.toiletNumber,
              badNums: roomInfo.badNums + '床位',
              roomBedClassificationList: room.roomBedClassificationList,
              checkNumber: '宜住' + roomInfo.checkNumber + '人',
              whetherAddBed: roomInfo.whetherAddBed,
              facilities: room.roomSupportingFacilitiesList,
              ffL: ffL,
              personalityDescribe: roomInfo.personalityDescribe,
              insideDiscribe: roomInfo.insideDiscribe,
              landlord_portraitSrc: landlordInfo.portraitSrc,
              landlord_nickname: landlordInfo.nickname,
              landlord_type: landlordInfo.type,
              landlord_introduction: landlordInfo.introduction,
              user: user,
              latitude: roomInfo.latitude,
              longitude: roomInfo.longitude,
              location: [{
                iconPath: "../../images/geography_location.png",
                id: 0,
                latitude: roomInfo.latitude,
                longitude: roomInfo.longitude,
                width: 30,
                height: 30

              }],
              markers: [{
                iconPath: "../../images/geography_location.png",
                id: 0,
                latitude: roomInfo.latitude,
                longitude: roomInfo.longitude,
                width: 30,
                height: 30

              }],
              // map_enable: true,
            })
            var mKey = ['公交车站', '地铁站']
            that.baiduInit(mKey)
            if (user) {
              that.setData({
                user_evaluateCount: user.evaluateCount + '条评论：',
                user_headPortrait: user.headPortrait,
                user_info: user.nickname + '    ' + user.createTime,
                user_content: user.content
              })
            }
          }
        }
        wx.hideLoading()
      }
    })
  },

  /**
   * 房东要求
   */
  getLandlordAsk(asks) {
    var ldAsks = []
    if (asks.receptionSex != null) {
      ldAsks.push(asks.receptionSex)
    }
    if (asks.whetherPet != null) {
      ldAsks.push(asks.whetherPet)
    }
    if (asks.whetherSomking != null) {
      ldAsks.push(asks.whetherSomking)
    }
    if (asks.whetherParty != null) {
      ldAsks.push(asks.whetherParty)
    }
    if (asks.whetherCook != null) {
      ldAsks.push(asks.whetherCook)
    }
    if (asks.receptionAgeZero != null) {
      ldAsks.push(asks.receptionAgeZero)
    }
    if (asks.receptionAgeSix != null) {
      ldAsks.push(asks.receptionAgeSix)
    }
    if (asks.receptionAgeGuests != null) {
      ldAsks.push(asks.receptionAgeGuests)
    }

    this.setData({
      landlordAsks: ldAsks
    })

  },
  /**
   * 房东服务
   */
  getLandlordService(landlordServices) {
    console.log(landlordServices)
    var ldlen = landlordServices.length;
    var ldServices = []
    for (var i = 0; i < landlordServices.length; i++) {
      let itemService
      var state = landlordServices[i].slsId
      switch (state) {
        case 1:
          itemService = {
            src: landlordServiceImgs[0],
            name: landlordServiceNames[0]
          }
          break;
        case 2:
          itemService = {
            src: landlordServiceImgs[1],
            name: landlordServiceNames[1]
          }
          break;
        case 3:
          itemService = {
            src: landlordServiceImgs[2],
            name: landlordServiceNames[2]
          }
          break;
        case 4:
          itemService = {
            src: landlordServiceImgs[3],
            name: landlordServiceNames[3]
          }
          break;
        case 5:
          itemService = {
            src: landlordServiceImgs[4],
            name: landlordServiceNames[4]
          }
          break;
        case 6:
          itemService = {
            src: landlordServiceImgs[5],
            name: landlordServiceNames[5]
          }
          break;
        case 7:
          itemService = {
            src: landlordServiceImgs[6],
            name: landlordServiceNames[6]
          }
          break;
        case 8:
          itemService = {
            src: landlordServiceImgs[7],
            name: landlordServiceNames[7]
          }
          break;
        case 9:
          itemService = {
            src: landlordServiceImgs[8],
            name: landlordServiceNames[8]
          }
          break;
        case 10:
          itemService = {
            src: landlordServiceImgs[9],
            name: landlordServiceNames[9]
          }
          break;
        case 11:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[10]
          }
          break;
        case 12:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[11]
          }
          break;
        case 13:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[12]
          }
          break;
        case 14:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[13]
          }
          break;
        case 15:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[14]
          }
          break;
        case 16:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[15]
          }
          break;
        case 17:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[16]
          }
          break;
        case 18:
          itemService = {
            src: landlordServiceImgs[10],
            name: landlordServiceNames[17]
          }
          break;
        case 19:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
        case 20:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
        case 21:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
        case 22:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
        case 23:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
        case 24:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
        case 25:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
        case 26:
          itemService = {
            src: landlordServiceImgs[11],
            name: landlordServiceNames[18]
          }
          break;
      }
      ldServices.push(itemService)
    }

    this.setData({
      landlordServices: ldServices
    })
  },
  /**
   * 传递数据
   */
  getIntentData(options) {
    var roomId = options.roomId
    var roomTitle = options.roomTitle
    var phone = this.toLogin()
    this.setData({
      sriRid: roomId,
      username: phone
    })
  },
  /**
   * 是否登录
   */
  toLogin() {
    var phoneNumber = wx.getStorageSync('phoneNumber')
    if (typeof phoneNumber == 'undefined' || phoneNumber == '' || phoneNumber == null) {
      phoneNumber = ''
      isLogin = true
    } else {
      isLogin = false
    }
    return phoneNumber
  },
  /**
   * 百度地图数据初始化
   */
  baiduInit(querry) {
    var that = this
    var BMap = new bmap.BMapWX({
      // ak: 'ktzbz4Bz5VgH3vsjnZEcgxLwUuzBTo9r'
      ak: 'ZOiyPoSRkWCAERjMaGCXUgydqpIXN0Fp'
    })
    var fail = function(data) {
      console.log(data)
    };
    var success = function(data) {
      var wxMarkerData = that.data.location.concat(data.wxMarkerData);
      console.log('wxMarkData: ' + wxMarkerData)
      that.setData({
        markers: wxMarkerData,
        mmK: data.wxMarkerData
      });
      that.setData({
        latitude: wxMarkerData[0].latitude
      });
      that.setData({
        longitude: wxMarkerData[0].longitude
      });
    }
    // 发起POI检索请求 
    BMap.search({
      "query": querry,
      location: that.data.latitude + ',' + that.data.longitude,
      fail: fail,
      success: success,
      // 此处需要在相应路径放置图片文件 
      iconPath: '../../images/traffic_mark.png',
      // 此处需要在相应路径放置图片文件 
      iconTapPath: '../../images/transportation.png',
      width: 15,
      height: 15
    });
  },

  /**
   * 交通切换
   */
  traffic_click: function(e) {
    var index = this.data.mapSelect[0].state
    var pre = 'mapSelect[' + preMapSel + '].isSelected'
    var mm = 'mapSelect[' + index + '].isSelected'
    this.setData({
      [pre]: false,
      [mm]: true
    })
    preMapSel = index
    var mKey = ['公交车站', '地铁站']
    this.baiduInit(mKey)
  },
  /**
   * 景点切换
   */
  scenary_click: function(e) {
    var index = this.data.mapSelect[1].state
    var pre = 'mapSelect[' + preMapSel + '].isSelected'
    var mm = 'mapSelect[' + index + '].isSelected'
    this.setData({
      [pre]: false,
      [mm]: true
    })
    preMapSel = index
    this.baiduInit('景点')
  },
  /**
   * 餐饮切换
   */
  repast_click: function(e) {
    var index = this.data.mapSelect[2].state
    var pre = 'mapSelect[' + preMapSel + '].isSelected'
    var mm = 'mapSelect[' + index + '].isSelected'
    this.setData({
      [pre]: false,
      [mm]: true
    })
    preMapSel = index
    this.baiduInit('美食')
  },
  /**
   * 生活切换
   */
  lives_click: function(e) {
    var index = this.data.mapSelect[3].state
    var pre = 'mapSelect[' + preMapSel + '].isSelected'
    var mm = 'mapSelect[' + index + '].isSelected'
    this.setData({
      [pre]: false,
      [mm]: true
    })
    preMapSel = index
    this.baiduInit('超市')
  },
  /**
   * 房东切换
   */
  landlord_check: function(e) {
    var that = this
    wx.navigateTo({
      url: '../landlord/landlord?sliId=' + that.data.landlordInfo.sliId,
    })
  },

  more_evaluate: function(e) {
    var that = this
    wx.navigateTo({
      url: '../more_envaluate/more_envaluate?roomId=' + that.data.sriRid,
    })
  },

  /**
   * 预定房屋
   */
  reserveRoom: function(e) {
    var that = this
    if (isLogin) {
      wx.navigateTo({
        url: '../loginAndRegister/loginAndRegister?jumpType=' + 3,
      })
    } else {
      wx.navigateTo({
        url: '../make_order/make_order?roomId=' + that.data.sriRid + '&username=' + that.data.username,
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var phone = this.toLogin()
    this.setData({
      username: phone
    })
    if (this.data.isDess) { //是否下单成功后重新进来
      wx.showLoading({
        title: '加载中...',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})